package org.smoozik.app.common.partition.component;

import java.awt.Graphics;
import java.util.ArrayList;

import org.smoozik.services.musictheory.Chord;
import org.smoozik.services.musictheory.Token;
import org.smoozik.services.musictheory.Interval;
import org.smoozik.services.musictheory.Note;

@SuppressWarnings("serial")
public class UIChord extends UIMusicComponent {

	public UIChord(Chord chord, UIPartition partition) {
		super(partition);
		this.token = new Token(chord);
	}

	public UIChord(Token t) {
		super(t);
	}

	// public UIChord(Token t, UIMeasure measure) {
	// super(t, measure);
	// }

	public void paint(Graphics g) {
		Chord chord = (Chord) token.getFigure();
		Note note = chord.getFondamentale();
		UINote paintedNote = null;
		ArrayList<Interval> relativeIntervalles = chord.getRelativeIntervals();
		int first = 0;
		int last = 0;

		if (relativeIntervalles != null) {
			for (int i = 0; i < relativeIntervalles.size(); i++) {
				note = note.computeIntervalle(relativeIntervalles.get(i));
				paintedNote = new UINote(note, partition);
				paintedNote.getToken().setDuration(token.getDuration());
				paintedNote.setWriteRythmBars(false);
				paintedNote.setLocation(x, 0);
				paintedNote.paint(g);

				if (first == 0) {
					first = paintedNote.getAbsoluteHauteur();
				}
			}
		}

		last = paintedNote.getAbsoluteHauteur();

		int xBar = x + partition.getInterval();
		int yBar = first + partition.getInterval() / 2;
		height = 2 * partition.getInterval() - (last - first);
		y = yBar - height;

		if (token.getDuration() < 4) {
			g.drawLine(xBar, yBar, xBar, y);
		}
		circle(g);
		// label = token.getDecomposedDurationLabel();

		 paintLabel(g, 10);
	}

}
